// components/uploadImage/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        imageNum: {
            type: Number,
            value: 9
        },
        imageList: {
            type: Array,
            value: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {},

    /**
     * 组件的方法列表
     */
    methods: {
        upload() {
            const _this = this
            let { imageNum, imageList } = this.data
            let num = imageNum - imageList.length
            wx.chooseImage({
                count: num <= 0 ? 0 : num,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success(res) {
                    // tempFilePath可以作为img标签的src属性显示图片
                    const tempFilePaths = res.tempFilePaths
                    _this.setData({
                        imageList: imageList.concat(tempFilePaths)
                    })
                }
            })
        },
        previewImage(e) {
            let index = e.target.dataset.index
            let { imageList } = this.data
            wx.previewImage({
                current: imageList[index],
                urls: imageList
            })
        },
        remove(e) {
            const _this = this
            let index = e.target.dataset.index
            let { imageList } = this.data
            wx.showModal({
                title: '提示',
                content: '您确定要删除本张图片吗？',
                success(res) {
                    if (res.confirm) {
                        imageList.splice(index, 1)
                        _this.setData({
                            imageList
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    }
})
